package cn.xz.xy.dialog

import android.view.Gravity
import android.view.animation.DecelerateInterpolator
import cn.xz.xy.R
import cn.xz.xy.base.BaseDialogRxFragment
import cn.xz.xy.bean.AlLNowData
import cn.xz.xy.bean.EventBusMessage
import cn.xz.xy.widgets.AutoScaleTextView
import cn.xz.xy.widgets.LinePercentBar

/**
 * 全部操作正在进行中弹窗
 * */
class AllNowDialog: BaseDialogRxFragment(true) {

    private var tvNowTxt: AutoScaleTextView? = null
    private var lpbBar: LinePercentBar? = null
    //进度条最大进度
    private var maxNum: Int = 0

    companion object {

        const val isOpen = "Open"

        const val isClose = "Close"

        fun newInstance(
            maxNum: Int
        ): AllNowDialog {
            val mDialog = AllNowDialog()
            mDialog.maxNum = maxNum
            return mDialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_now_all
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 400)
    }

    override fun initViews() {

        dialog?.let {
            tvNowTxt = it.findViewById(R.id.tv_now_txt)
            lpbBar = it.findViewById(R.id.lpb_bar)
        }

        //点击外部不隐藏
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        //视图
        initUI()
    }

    private fun initUI(){
        if (maxNum != 0){
            //强制清零
            lpbBar?.clearNumber()
            //设置渐变色
            lpbBar?.setChangeColor("#61ABFF","#68FAF1")
            //设置最大值
            lpbBar?.setBigData(maxNum.toFloat(),false)
        }
    }

    override fun initEventBus(message: EventBusMessage) {
        super.initEventBus(message)
        val mData = message.messageBody as AlLNowData
        when(message.messageTag){
            isOpen ->{
                //当前开启
                tvNowTxt?.text = "正在开启${mData.name}"
                //当前进度
                lpbBar?.setPercentData(mData.curData!!.toFloat(), DecelerateInterpolator())
                //如果当前进度等于最大值，销毁弹窗
                if (mData.curData == maxNum){
                    tvNowTxt?.postDelayed({
                        dismiss()
                    },500)
                }
            }
            isClose ->{
                //当前关闭
                tvNowTxt?.text = "正在关闭${mData.name}"
                //当前进度
                lpbBar?.setPercentData(mData.curData!!.toFloat(), DecelerateInterpolator())
                //如果当前进度等于最大值，销毁弹窗
                if (mData.curData == maxNum){
                    tvNowTxt?.postDelayed({
                        dismiss()
                    },500)
                }
            }
        }
    }
}